=========================================================================== BBS: The Abacus * HST/DS * Potterville, MI Date: 03-12-93 (09:14) Number: 141 From: CHRIS TRACY Refer#: NONE To: CALVIN FRENCH Recvd: NO Subj: Badly Needed! Bin2string Conf: (35) Quick Basi --------------------------------------------------------------------------- While trying to compile a QB program to less than 4k, Calvin French spoke... > i've got your attention, I need something VERY badly. It's a > relatively FAST decimal 2 binary function, as well as a binary 2 > decimal function to go with it. The binary function needs to be > put in a string ($), so that I can deal with it. I'm not just ---- Cut Here /w Sharp Razorblades ---- Oh allright... here it goes!! ----- Cut Here ----- DECLARE FUNCTION BinDec& (Binary$) DECLARE FUNCTION DecBin$ (decimal&) Bin$ = DecBin$(5050) ' Convert 5050 to binary PRINT Bin$ ' Print the result PRINT BinDec&(Bin$) ' Convert 0001001110111010 to the decimal equiv. FUNCTION BinDec& (Binary$) STATIC decimal& = 0: power% = 0 Binary$ = UCASE$(Binary$) FOR i% = LEN(Binary$) TO 1 STEP -1 digit% = ASC(MID$(Binary$, i%, 1)) - 48 IF digit% < 0 OR digit% > 1 THEN decimal& = 0: EXIT FOR decimal& = decimal& + digit% * 2 ^ (power%) power% = power% + 1 NEXT i% BinDec& = decimal& END FUNCTION FUNCTION DecBin$ (decimal&) STATIC Bin$ = "" h$ = HEX$(decimal&) FOR i% = 1 TO LEN(h$) digit% = INSTR("0123456789ABCDEF", MID$(h$, i%, 1)) - 1 IF digit% < 0 THEN Bin$ = "": EXIT FOR j% = 8: k% = 4 DO Bin$ = Bin$ + RIGHT$(STR$((digit% \ j%) MOD 2), 1) j% = j% - (j% \ 2): k% = k% - 1 IF k% = 0 THEN EXIT DO LOOP WHILE j% NEXT i% DecBin$ = Bin$ END FUNCTION ----- Cut Here ----- Hope that didn't get too chopped.... Anywaz, that is an EXTREMLY fast routine... Its about as fast as your gonna get in QB... -chris --- T.A.G. 2.6d Standard * Origin: DangerBase ][ Programming Staff 412-438-4101 (1:2615/4@FIDONET.ORG) SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1 SEEN-BY: 153/752 154/40 77 157/2 159/100 125 430 950 203/23 209/209 280/1 SEEN-BY: 390/1 396/1 15 397/2 2230/100 3603/20